#!/usr/bin/env python
# @Date    : 2022-02-29
# @Author  : Bright (brt2@qq.com)
# @Link    : https://gitee.com/brt2
# @Version : 0.1.0

import os
import json
from mkm import dump_km_as_mkm

def km_to_mkm(path_dir):
    for root, dirs, files in os.walk(path_dir, topdown=False):
        for name in files:
            path_file = os.path.join(root, name)
            filename, ext = os.path.splitext(path_file)
            if ext == ".km":
                path_mkm = filename + ".mkm"
                if not os.path.exists(path_mkm):
                    with open(path_file, "r", encoding="utf8") as fp:
                        dict_km = json.load(fp)
                    dump_km_as_mkm(dict_km, path_mkm)
                os.remove(path_file)  # 删除km文件
        for name in dirs:
            path_subdir = os.path.join(root, name)
            km_to_mkm(path_subdir)


if __name__ == "__main__":
    km_to_mkm("../mindmap")
